package com.weiqk.pmdemo.service.impl;

import com.weiqk.pmdemo.dao.PayLinkRepostitory;
import com.weiqk.pmdemo.dao.ProjectRepository;
import com.weiqk.pmdemo.dao.PurchaseLinkRespository;
import com.weiqk.pmdemo.dao.PurchaseRepository;
import com.weiqk.pmdemo.dto.PayLinkInDto;
import com.weiqk.pmdemo.dto.PurchaseInDto;
import com.weiqk.pmdemo.dto.PurchaseLinkInDto;
import com.weiqk.pmdemo.pojo.PayLink;
import com.weiqk.pmdemo.pojo.Project;
import com.weiqk.pmdemo.pojo.Purchase;
import com.weiqk.pmdemo.pojo.PurchaseLink;
import com.weiqk.pmdemo.service.PayLinkService;
import com.weiqk.pmdemo.service.PurchaseLinkService;
import com.weiqk.pmdemo.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {

    @Autowired
    PurchaseRepository purchaseRepository;
    @Autowired
    PayLinkRepostitory payLinkRepostitory;
    @Autowired
    PurchaseLinkRespository purchaseLinkRespository;
    @Autowired
    PayLinkService payLinkService;
    @Autowired
    PurchaseLinkService purchaseLinkService;
    @Autowired
    ProjectRepository projectRepository;


    @Override
    public Iterable<Purchase> findAll() {
//        purchaseRepository.findAll();
        return purchaseRepository.findAll();
    }
    //不能直接删除,判断是否被引用
    @Override
    public Boolean deleteById(int id) {
        boolean pay = payLinkRepostitory.findById(id).isPresent();
        boolean purchaseLink = purchaseLinkRespository.findById(id).isPresent();
        if (pay){
            return false;
        }
        if (purchaseLink){
            return false;
        }
        purchaseRepository.deleteById(id);

        return true;
    }
    //更改的话pay和purchaselink都要更改，先更改引用的表
    @Override
    public Boolean update(PurchaseInDto purchaseInDto) {
        //更改purchaselink数据
        List<PurchaseLink> pls = purchaseLinkRespository.findOneById(purchaseInDto.getPurchase().getPurchaseId());
        for (PurchaseLink pl:pls){
            PurchaseLinkInDto pldto = new PurchaseLinkInDto();
            pldto.setPurchaseLink(pl);
            pldto.setPurchaseNum(purchaseInDto.getPurchase().getPurchaseId());
            purchaseLinkService.update(pldto);
        }

        //更改paylink数据
        List<PayLink> pys = payLinkRepostitory.findOneById(purchaseInDto.getPurchase().getPurchaseId());
        for (PayLink py:pys){
            PayLinkInDto pydto=new PayLinkInDto();
            pydto.setPayLink(py);
            pydto.setPurchaseNum(purchaseInDto.getPurchase().getPurchaseId());
            payLinkService.update(pydto);
        }

        //执行更新
        //处理时间
        Purchase purchase = purchaseInDto.getPurchase();
        purchase.setUpadteTime(new Date());
        //处理外键数据
        int projectNum = purchaseInDto.getProjectNum();
        Project project = projectRepository.findOneById(projectNum);
        purchase.setProject(project);
        //操作数据库
        purchaseRepository.save(purchase);
        return true;

    }

    @Override
    public Boolean put(PurchaseInDto purchaseInDto) {
        //处理时间
        Purchase purchase = purchaseInDto.getPurchase();
        purchase.setUpadteTime(new Date());
        purchase.setCreateTime(new Date());
        //处理外键数据
        int projectNum = purchaseInDto.getProjectNum();
        Project project = projectRepository.findOneById(projectNum);
        purchase.setProject(project);
        //操作数据库
        purchaseRepository.save(purchase);
        return true;
    }

    @Override
    public List<Purchase> findfalgnull() {
        return purchaseRepository.findfalgnull();
    }

    @Override
    public int countneednum(int id) {
        List<PayLink> payLinks = payLinkRepostitory.findOneById(id);
        //一个采购情况对应多少金额
        int sum = payLinks.stream().mapToInt(PayLink::getAmount).sum();
        int needallsum = purchaseRepository.findAproject(id);
        return  needallsum-sum;

    }

    @Override
    public PayLink firstPay(int id) {
        return payLinkRepostitory.findfirstPay(id).get(0);
    }
}
